<script lang="ts" setup>
import {LockClosedOutline, PersonOutline} from "@vicons/ionicons5";
import {reactive, ref} from "vue";
import {useUserStore} from "@store/user.ts";
import {useRouter} from "vue-router";


const formRef = ref();
const loading = ref(false);
const userStore = useUserStore();
const router = useRouter();

interface FormState {
  email: string,
  password: string
}

// 获取表项中收集到的值的对象
const formInline = reactive({
  email: 'super@a.com',
  password: '123123',
});

// 验证表项的规则
const rules = {
  email: {required: true, message: '请输入用户名', trigger: 'blur'},
  password: {required: true, message: '请输入密码', trigger: 'blur'},
};

const handleSubmit = (e: MouseEvent) => {
  e.preventDefault();
  // 验证表单
  formRef.value.validate(async (errors: boolean) => {
    if (errors) {
      return;
    }
    // 接收数据
    const {email, password} = formInline
    // 显示加载
    loading.value = true;
    // 调整数据结构
    const data: FormState = {
      email,
      password
    }
    // 执行登录操作
    userStore.login(data).then((res: any) => {
      // 关闭加载
      loading.value = false;
      // 弹出成功提示
      alert('success');
      // 判断当前页面的 url 是否存在 redirect，如果存在则跳转到 redirect 对应的页面，否则跳转到首页
      const redirect = router.currentRoute.value.query.redirect as string
      if (redirect) {
        router.push({path: redirect}
        )
      } else {
        router.push({name: 'home'})
      }
    }).catch(() => {
      // 关闭加载 关闭加载
      loading.value = false;
      // 弹出失败提示
      alert('fail');
    })
  })
}

</script>

<template>
  <div class="view-account">
    <div class="view-account-header"></div>
    <div class="view-account-container">
      <div class="view-account-top">
        <div class="view-account-top-logo m-0">
          <img alt="" class="m-auto" src="@assets/images/logo.png"/>
        </div>
      </div>
      <div class="view-account-form">
        <n-form
            ref="formRef"
            :model="formInline"
            :rules="rules"
            label-placement="left"
            size="large"
        >
          <n-form-item path="username">
            <n-input v-model:value="formInline.email" placeholder="请输入用户名">
              <template #prefix>
                <n-icon color="#808695" size="18">
                  <PersonOutline/>
                </n-icon>
              </template>
            </n-input>
          </n-form-item>
          <n-form-item path="password">
            <n-input v-model:value="formInline.password" placeholder="请输入密码" showPasswordOn="click"
                     type="password">
              <template #prefix>
                <n-icon color="#808695" size="18">
                  <LockClosedOutline/>
                </n-icon>
              </template>
            </n-input>
          </n-form-item>
          <n-form-item>
            <n-button :loading="loading" block size="large" type="primary" @click="handleSubmit">
              登录
            </n-button>
          </n-form-item>
        </n-form>
      </div>
    </div>
  </div>
</template>


<style lang="less" scoped>
.view-account {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: auto;

  &-container {
    flex: 1;
    padding: 32px 12px;
    max-width: 384px;
    min-width: 320px;
    margin: 0 auto;
  }

  &-top {
    padding: 32px 0;
    text-align: center;

    &-desc {
      font-size: 14px;
      color: #808695;
    }
  }

  &-other {
    width: 100%;
  }

  .default-color {
    color: #515a6e;

    .ant-checkbox-wrapper {
      color: #515a6e;
    }
  }
}

@media (min-width: 768px) {
  .view-account {
    background-image: url('../../assets/images/login.svg');
    background-repeat: no-repeat;
    background-position: 50%;
    background-size: 100%;
  }

  .page-account-container {
    padding: 32px 0 24px 0;
  }
}
</style>